package com.cabinet.service.utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.stereotype.Component;
@Component
public class SmsClient {

	/**
	 * webservice服务器定义
	 */
	// 调用注册方法可能不成功。
	// java.io.IOException: Server returned HTTP response code: 400 for URL:
	// 如果出现上述400错误，请参考第102行。
	// 如果您的系统是utf-8，收到的短信可能是乱码，请参考第102，295行
	// 可以根据您的需要自行解析下面的地址
	// http://sdk2.zucp.net:8060/webservice.asmx?wsdl
	private String serviceURL = "http://124.172.234.157:8180/Service.asmx";
	/**
	 * 方法名称：mt 功 能：发送短信 ,传多个手机号就是群发，一个手机号就是单条提交 参
	 * 数：mobile,content,ext,stime,rrid(手机号，内容，扩展码，定时时间，唯一标识) 返 回
	 * 值：唯一标识，如果不填写rrid将返回系统生成的
	 * 
	 * @throws Exception
	 */
	public String mt(String mobile, String content) throws Exception {
		String result = "";
		String soapAction = "http://tempuri.org/SendMessage";
		String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
		xml += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
		xml += "<soap:Body>";
		xml += "<SendMessage xmlns=\"http://tempuri.org/\">";
		xml += "<Id>300</Id>";
		xml += "<Name>chuangke</Name>";
		xml += "<Psw>8888</Psw>";
		xml += "<Message>" + content + "</Message>";
		xml += "<Phone>" + mobile + "</Phone>";
		xml += "<Timestamp>0</Timestamp>";
		xml += "</SendMessage>";
		xml += "</soap:Body>";
		xml += "</soap:Envelope>";

		URL url;

		url = new URL(serviceURL);

		URLConnection connection = url.openConnection();
		HttpURLConnection httpconn = (HttpURLConnection) connection;
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		// bout.write(xml.getBytes());

		bout.write(xml.getBytes("utf-8"));
		byte[] b = bout.toByteArray();
		httpconn.setRequestProperty("Content-Length", String.valueOf(b.length));
		httpconn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");// 这一句也关键
		httpconn.setRequestProperty("SOAPAction", soapAction);
		httpconn.setRequestMethod("POST");
		httpconn.setDoInput(true);
		httpconn.setDoOutput(true);

		OutputStream out = httpconn.getOutputStream();
		out.write(b);
		out.close();

		InputStreamReader isr = new InputStreamReader(httpconn.getInputStream());
		BufferedReader in = new BufferedReader(isr);
		String inputLine;
		while (null != (inputLine = in.readLine())) {
			Pattern pattern = Pattern.compile("<SendMessageResult>(.*)</SendMessageResult>");
			Matcher matcher = pattern.matcher(inputLine);
			while (matcher.find()) {
				result = matcher.group(1);
			}
		}
		if (result.startsWith("-") || result.equals(""))// 以负号判断是否发送成功
		{
			throw new Exception(SmsErrorCode.getErrorCodeString(result));
		}
		return result;

	}

}

